﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using UnityEngine.UI;

public class loadcard : MonoBehaviour
{
    //public JsonData cardtoadd;
    //public List<cardtosave1> cardtoadd1 = new List<cardtosave1>();
    private cardcontrol cardinfo;

    void Start()
    {
        cardinfo = GameObject.Find("cardmanager").GetComponent<cardcontrol>();
    }

    void destoryslots()
    {
        for (int j = 0; j < cardinfo.slots.Count; j++)
        {

            if (cardinfo.slots[j].transform.childCount > 0)
            {
                carddata data = cardinfo.slots[j].transform.GetChild(0).GetComponent<carddata>();
                for (int i = 0; i < 4; i++)
                {
                    data.amount--;
                    if (data.amount == 0)
                    {
                        Destroy(cardinfo.slots[j].transform.GetChild(0).gameObject);

                    }

                }
            }
        }
        cardinfo.cards.Clear();
        for (int i = 0; i < cardinfo.slotAmount; i++)
        {
            cardinfo.cards.Add(new Card());
        }
    }

    //void cardtoadd1con()
    //{
    //    for (int i = 0; i < cardtoadd.Count; i++)
    //    {
    //        cardtoadd1.Add(new cardtosave1(cardtoadd[i]["name"].ToString(), (int)cardtoadd[i]["amount"]));
    //    }
    //}

    //void cardtoaddadd()
    //{
    //    for (int i = 0; i < cardtoadd1.Count;i++)
    //    {
    //        cardinfo.AddCard(cardtoadd1[i].name);
    //        Debug.Log("1");
    //        carddata savedcardamount1 = cardinfo.slots[i].transform.GetChild(0).GetComponent<carddata>();
    //        savedcardamount1.amount = cardtoadd1[i].amount;
    //        cardinfo.slots[i].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = savedcardamount1.amount.ToString();
    //        //Debug.Log("2");
    //    }
    //}

    public void onclickbutton()
    {
        destoryslots();
        //cardtoadd = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/cards/1.json"));
        //cardtoadd1con();
        //cardtoaddadd();
        //cardtoadd.Clear();
        //cardtoadd1 = new List<cardtosave1>();
    }

}
